<template>
  <div class="bgff p20">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="网关名">
        <el-select v-model="gatewayId" placeholder="请选择" clearable>
          <el-option v-for="item in gatewaylist" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="medium" type="primary" @click="search">查询</el-button>
        <el-button size="medium" type="primary" @click="reset">重置刷新</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      v-loading="loading"
      row-key="id"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        :index="(index)=>{return (index+1) + (curPage - 1) * size }"
        label="序号"
        type="index"
        width="50"
      ></el-table-column>
      <el-table-column prop="name" label="设备名字"></el-table-column>
      <el-table-column prop="gateway_address" label="地址"></el-table-column>
      <el-table-column prop="equipmentGateways.name" label="所属网关">
         <template slot-scope="scope">{{scope.row.gateways?scope.row.gateways.name:'-'}}</template>
      </el-table-column>
      <el-table-column prop="current.a_i" label="电流（A）">
        <template slot-scope="scope">{{scope.row.current.a_i=='-'?scope.row.current.a_i:(scope.row.current.a_i/10).toFixed(1)}}</template>
      </el-table-column>
      <el-table-column prop="current.a_v" label="电压（V）">
        <template slot-scope="scope">{{scope.row.current.a_v=='-'?scope.row.current.a_v:scope.row.current.a_v/10}}</template>
      </el-table-column>
      <el-table-column prop="current.a_ww" label="电量（kW·h）"></el-table-column>
      <el-table-column prop="current.a_t" label="温度（℃）"></el-table-column>
      <el-table-column prop="current.a_di" label="剩余电流（mA）"></el-table-column>
      <el-table-column prop="current.a_pw" label="瞬时功率（kW）">
        <template slot-scope="scope">{{scope.row.current.a_pw=='-'?scope.row.current.a_pw:scope.row.current.a_pw/10000}}</template>
      </el-table-column>
      <el-table-column prop="current.a_ss" label="功率因素">
        <template slot-scope="scope">{{scope.row.current.a_ss=='-'?scope.row.current.a_ss:scope.row.current.a_ss/100}}</template>
      </el-table-column>
      <el-table-column label="采集时间">
        <template
          slot-scope="scope"
        >{{scope.row.current.create_time?new Date(scope.row.current.create_time).format('yyyy-MM-dd hh:mm:ss'):'-'}}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="goShow(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="mt20"
      @current-change="handlePage"
      @size-change="handleSizeChange"
      :current-page.sync="curPage"
      :page-size="size"
      :page-sizes="[5,10]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { _eqmtList, _gatewaysAllGet, _equipmentShow } from "@/http/ajax";
export default {
  data() {
    return {
      loading: false,
      dialog: false,
      formInline: {
        user: "",
        region: ""
      },
      tableData: [],
      curPage: 1,
      size: 10,
      total: 0,
      gatewaylist: [],
      gatewayId: ""
    };
  },
  created() {
    this.getGatewaysAll();
  },
  methods: {
    // 获取设备列表
    getList() {
      let data = {
        page: this.curPage - 1,
        size: this.size,
        level: sessionStorage.level,
        gatewayId: this.gatewayId||"",
        type: sessionStorage.level == 5 ? "5" : "",
        sort: "id,desc"
      };
      this.loading = true;
      this.tableData = [];
      _eqmtList(
        data,
        res => {
          this.total = res.totalElements;
          // this.tableData = res.content;
          let btnall = {};
          res.content.forEach(el => {
            btnall["id_" + el.id] = false;
            _equipmentShow(el.id, ress => {
              el["current"] = ress.current || {
                a_di: "-",
                a_i: "-",
                a_pw: "-",
                a_ss: "-",
                a_t: "-",
                a_v: "-",
                a_vw: "-",
                a_ww: "-",
                b_di: "-",
                b_i: "-",
                b_pw: "-",
                b_ss: "-",
                b_t: "-",
                b_v: "-",
                b_vw: "-",
                b_ww: "-",
                c_di: "-",
                c_i: "-",
                c_pw: "-",
                c_ss: "-",
                c_t: "-",
                c_v: "-",
                c_vw: "-",
                c_ww: "-"
              };
              el["loudian_count"] = ress.loudian_count;
              btnall["id_" + el.id] = true;
            });
          });
          this.loopCheck(btnall, res);
        },
        err => {}
      );
    },
    /**
     * 递归检测
     */
    loopCheck(boo, res) {
      let rebtn = false;
      Object.keys(boo).forEach(key => {
        if (!boo[key]) {
          rebtn = true;
        }
      });
      if (rebtn) {
        setTimeout(() => {
          this.loopCheck(boo, res);
        }, 300);
      } else {
        this.tableData = res.content;
        setTimeout(() => {
          this.loading = false;
        }, 600);
      }
    },
    // 获取所有网关
    getGatewaysAll() {
      let user = JSON.parse(sessionStorage.user);
      let data = {
        deptId: user.deptId || user.dept.id || "",
        level: sessionStorage.level
      };
      _gatewaysAllGet(
        data,
        res => {
          this.gatewaylist = res;
          this.getList();
        },
        err => {}
      );
    },
    goShow(id) {
      let routeUrl = this.$router.resolve({
        path: `/eqmtShow/${id}`
        //   query: {id:row.id}
      });
      window.open(routeUrl.href, "_blank");
    },
    search() {
      this.curPage = 1;
      this.getList();
    },
    reset() {
      this.curPage = 1;
      this.gatewayId = "";
      this.size = 10;
      this.getList();
    },
    // 切换页码
    handlePage(val) {
      this.curPage = val;
      this.getList();
    },
    // 切换条数
    handleSizeChange(val) {
      this.size = val;
      this.getList();
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
